<?php
/**
 * Created by PhpStorm.
 * User: peter
 * Date: 2018/10/19
 * Time: 下午1:24
 */

namespace app\activity\logic;

use app\model\ActivityGroupStage;
use app\model\ActivityOrder;
use app\model\ActivityOrderAttach;
use app\model\ActivityOrderRefund;
use extend\Functions;
use extend\STATUS_CODE;
use think\Db;

class Order
{
    /**
     * @purpose 申请取消已支付订单
     * @param string $order_no
     * @param string $reason
     * @return array
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function cancelByPay(string $order_no, string $reason, int $uid): array
    {
        $order        = new \app\model\ActivityOrder();
        $order_attach = new ActivityOrderAttach();
        $order_refund = new ActivityOrderRefund();
        $order->startTrans();
        $order_info = $order
            ->lock(true)
            ->where(['order_no' => $order_no, 'uid' => $uid])
            ->field('id,pay_money,pay_no,pay_voucher,order_no,pay_method,status,activity_id,group_stage_id,uid')
            ->find();
        if (!$order_info) {
            $order->rollback();
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '订单不存在...!'
            );
        }
        if ($order_info->status !== 99) {
            $order->rollback();
            return Functions::result(
                STATUS_CODE::ORDER_STATUS_ERROR,
                '当前状态不可取消并退款'
            );
        }
        $refund_money     = $order_attach
            ->lock(true)
            ->where([
                'order_id' => $order_info->id,
                'status'   => 99,
            ])
            ->sum('price');
        $order_attach_ids = $order_attach
            ->isUpdate(false)
            ->where([
                'order_id' => $order_info->id,
                'status'   => 99,
            ])
            ->field('id')
            ->select();

        $_order_attach_ids = [];
        array_map(function ($item) use (&$_order_attach_ids) {
            array_push($_order_attach_ids, $item['id']);
        }, $order_attach_ids->toArray());

        $order_attach_result = $order_attach
            ->isUpdate(true)
            ->save(['status' => 4, 'cancel_reason' => $reason], [
                'order_id' => $order_info->id,
                'status'   => 99,
            ]);
        // 构建订单退款数据
        $order_refund_data                    = [];
        $order_refund_data['order_id']        = $order_info->id;
        $order_refund_data['order_attach_id'] = implode(',', $_order_attach_ids);
        $order_refund_data['activity_id']     = $order_info->activity_id;
        $order_refund_data['group_stage_id']  = $order_info->group_stage_id;
        $order_refund_data['order_no']        = $order_info->order_no;
        $order_refund_data['refund_reason']   = $reason;
        $order_refund_data['refund_money']    = $refund_money;
        $order_refund_data['pay_no']          = $order_info->pay_no;
        $order_refund_data['pay_voucher']     = $order_info->pay_voucher;
        $order_refund_data['pay_method']      = $order_info->pay_method;
        $order_refund_data['pay_money']       = $order_info->pay_money;
        $order_refund_data['refund_no']       = date('YmdHis') . Functions::random(18);
        RE_REFUND_NO:
        $order_refund_data['refund_no'] = date('YmdHis') . Functions::random(18);
        if ($order_refund->where('refund_no', $order_refund_data['refund_no'])->count(1) > 0) {
            goto RE_REFUND_NO;
        }
        $order_refund_data['status'] = 1;
        $order_refund_data['uid']    = $order_info->uid;
        $order_refund_result = $order_refund
            ->isUpdate(false)
            ->data($order_refund_data)
            ->save();
        $order_result   = $order
            ->isUpdate(true)
            ->save(['status' => 5],['id' => $order_info->id]);
        if ($order_refund_result &&
            $order_attach_result &&
            $order_result) {
            $order_attach->commit();
            return Functions::result();
        }
        $order_attach->rollback();
        return Functions::result(
            STATUS_CODE::FAIL,
            '申请失败'
        );
    }

    /**
     * @purpose 取消出行人（支付之后）
     * @param int $attach_id
     * @param string $reason
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function cancelTravelByPay(
        int $attach_id,
        string $reason): array
    {
        $order = new ActivityOrder();
        $order_attach = new ActivityOrderAttach();
        $order_refund = new ActivityOrderRefund();
        $order_attach->startTrans();
        $order_attach_info = $order_attach
            ->lock(true)
            ->alias('aoa')
            ->join('activity_order ao', 'ao.id=aoa.order_id')
            ->where('aoa.id', $attach_id)
            ->field('aoa.id,aoa.order_id,ao.pay_money,ao.pay_no,ao.pay_voucher,ao.pay_method,aoa.price,aoa.activity_id,aoa.group_stage_id,aoa.status,aoa.group_stage_id,ao.uid,ao.order_no')
            ->find();
        if ($order_attach_info->status !== 99) {
            $order_attach->rollback();
            return Functions::result(
                STATUS_CODE::FAIL,
                '当前状态不可取消'
            );
        }
        // 修改出行人状态
        $order_attach_result = $order_attach
            ->isUpdate(true)
            ->save(
                ['status' => 4, 'cancel_reason' => $reason],
                ['id' => $attach_id]
            );
        // 构建订单退款数据
        $order_refund_data                    = [];
        $order_refund_data['order_id']        = $order_attach_info->order_id;
        $order_refund_data['order_attach_id'] = $attach_id;
        $order_refund_data['activity_id']     = $order_attach_info->activity_id;
        $order_refund_data['group_stage_id']  = $order_attach_info->group_stage_id;
        $order_refund_data['order_no']        = $order_attach_info->order_no;
        $order_refund_data['refund_reason']   = $reason;
        $order_refund_data['refund_money']    = $order_attach_info->price;
        $order_refund_data['pay_no']          = $order_attach_info->pay_no;
        $order_refund_data['pay_voucher']     = $order_attach_info->pay_voucher;
        $order_refund_data['pay_method']      = $order_attach_info->pay_method;
        $order_refund_data['pay_money']      = $order_attach_info->pay_money;
        $order_refund_data['refund_no']       = date('YmdHis') . Functions::random(18);
        RE_REFUND_NO:
        $order_refund_data['refund_no'] = date('YmdHis') . Functions::random(18);
        if ($order_refund->where('refund_no', $order_refund_data['refund_no'])->count(1) > 0) {
            goto RE_REFUND_NO;
        }
        $order_refund_data['status'] = 1;
        $order_refund_data['uid']    = $order_attach_info->uid;

        $order_refund_result = $order_refund
            ->isUpdate(false)
            ->data($order_refund_data)
            ->save();
        // 检测当前订单是否全部申请退款
        $order_result = true;
        if ($order_attach->where([
            'status' => 99,
            'order_id' => $order_attach_info->order_id
        ])->count(1) < 1) {
            $order_result = $order
                ->isUpdate(true)
                ->save(['status' => 5], ['id' => $order_attach_info->order_id]);
        }

        if ($order_refund_result && $order_attach_result && $order_result) {
            $order_attach->commit();
            return Functions::result();
        }
        $order_attach->rollback();
        return Functions::result(
            STATUS_CODE::FAIL,
            '取消失败'
        );
    }

    /**
     * @purpose 订单未支付时取消出行人
     * @param int $attach_id
     * @param string $reason
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function cancelTravelByNotPay(
        int $attach_id,
        string $reason = ''
    ): array
    {
        $order        = new ActivityOrder();
        $order_attach = new ActivityOrderAttach();
        $order->startTrans();
        $order_attach_info = $order_attach
            ->lock(true)
            ->where('id', $attach_id)
            ->field('id,order_id,status')
            ->find();
        if ($order_attach_info->status !== 1) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '该状态不可取消'
            );
        }
        $order_attach_result = $order_attach
            ->isUpdate(true)
            ->save(
                ['status' => 2, 'cancel_reason' => $reason],
                ['id' => $attach_id]
            );
        $order_result        = true;
        if ($order_attach->where([
                'order_id' => $order_attach_info->order_id,
                'status'   => 1,
            ])->count(1) < 1) {
            $order_result = $order
                ->isUpdate(true)
                ->save(
                    ['status' => 4],
                    ['id' => $order_attach_info->order_id]);
        }
        if ($order_result && $order_attach_result) {
            $order->commit();
            return Functions::result();
        }
        $order->rollback();
        return Functions::result(
            STATUS_CODE::FAIL,
            '取消失败'
        );
    }

    /**
     * @purpose 未支付时取消订单
     * @param string $order_no
     * @param string $reason
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function cancelByNotPay(
        string $order_no = '',
        string $reason = '',
        int $uid): array
    {
        if (!$order_no) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误...!'
            );
        }
        $order        = new ActivityOrder();
        $order_attach = new ActivityOrderAttach();
        $order->startTrans();
        $order_info = $order
            ->lock(true)
            ->where(['order_no' => $order_no, 'uid' => $uid])
            ->field('id,uid,activity_id,status,order_no,pay_money')
            ->find();
        if (!$order_info) {
            $order->rollback();
            return Functions::result(
                STATUS_CODE::FAIL,
                '订单不存在...!'
            );
        }
        if ($order_info->status !== 1) {
            $order->rollback();
            return Functions::result(
                STATUS_CODE::ORDER_STATUS_ERROR,
                '订单状态错误...!'
            );
        }
        $order_result        = $order
            ->isUpdate(true)
            ->save(
                ['status' => 4],
                ['id' => $order_info->id]);
        $order_attach_result = $order_attach
            ->isUpdate(true)
            ->save(
                ['status' => 2, 'cancel_reason' => $reason],
                ['order_id' => $order_info->id]);
        if ($order_result && $order_attach_result) {
            $order->commit();
            return Functions::result();
        }
        $order->rollback();
        return Functions::result(
            STATUS_CODE::UPDATE_FAIL,
            '取消失败'
        );
    }

    /**
     * @purpose 订单详情
     * @param string $order_no
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function detail(string $order_no, int $uid): array
    {
        $model                = new ActivityOrder();
        $fields               = 'ao.id,ao.order_no,ao.status,ao.create_at,ao.activity_id,
        FROM_UNIXTIME(ags.start_time, "%Y-%m-%d") as start_date,ags.start_time,
        ags.adult_price,ags.kid_price,
        FROM_UNIXTIME(ags.end_time, "%Y-%m-%d") as end_date,
        FROM_UNIXTIME(ao.pay_time, "%Y-%m-%d %H:%i") as sign_up_date,
        (select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 1) as adult_num,
        (select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 2) as kid_num,
        (select sum(price) as `total_price` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id`) as total_price,
        (FLOOR(((ags.end_time - ags.start_time) / 86400)) + 1) as days';
        $result               = $model
            ->with(['attach' => function ($query) {
                $query
                    ->alias('at')
                    ->leftJoin('activity_collection_point acp', 'at.collection_point_id=acp.id')
                    ->field('at.id as order_attach_id,at.order_id,at.group_stage_id,at.collection_point_id,at.fullname,
                at.travel_type,at.travel_sex,at.travel_mobile,at.travel_certificate_type,at.travel_certificate_number,at.price,at.status,
                acp.title as collection_point_title,acp.address as collection_point_address');
            }, 'activity'    => function ($query) {
                $query
                    ->alias('a')
                    ->leftJoin('club c', 'c.uid=a.club_id')
                    ->field('a.id,a.title,a.thumb,a.descriptions,a.light_spot,a.club_id,c.name as club_name');
            }])
            ->alias('ao')
            ->leftJoin('activity_group_stage ags', 'ags.id=ao.group_stage_id')
//            ->leftJoin('club c', 'c.uid=a.club_id')
            ->where([
                ['ao.order_no', '=', $order_no],
                ['ao.uid', '=', $uid],
            ])
            ->field($fields)
            ->find();
        $result['start_week'] = Functions::weekday($result->start_time);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }

    /**
     * @purpose 获取订单列表
     * @param int $status
     * @param int $page
     * @param int $size
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function items(int $status = 1, int $page = 1, int $size = 10, int $uid): array
    {
        $model   = new ActivityOrder();
        $fields  = [
            'ao.id',
            'ao.activity_id',
            'ao.order_no',
            'ao.status',
            'FROM_UNIXTIME(ao.pay_time, "%Y-%m-%d %H:%i") as sign_up_date',
            'FROM_UNIXTIME(ao.create_at, "%Y-%m-%d %H:%i") as create_date',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 1)' => 'adult_num',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 2)' => 'kid_num',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 3 or `status` = 4 or `status` = 5) and `order_id`=`ao`.`id` and `travel_type` = 2)' => 'refund_num',
            '(select count(1) as `total` from `activity_order_attach` where `order_id`=`ao`.`id` and `travel_type` = 2)' => 'total_num',
            '(select sum(price) as `total_price` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id`)'               => 'total_price',
        ];
        $where   = [];
        $where[] = ['ao.uid', '=', $uid];
        if ($status === 1 || $status === 99) {
            $where[] = ['ao.status', '=', $status];
            $where[] = ['ags.start_time', '>', time()];
        } else if ($status === 100) {
            $where[] = ['ags.start_time', '<', time()];
        } else if ($status === 98) {
            return self::refundOrderItems($page, $size, $uid);
        }
        $list = $model
            ->with(['activity' => function ($query) {
                $query->field('id,title,descriptions,thumb,light_spot');
            }])
            ->alias('ao')
            ->leftJoin('activity_group_stage ags', 'ags.id=ao.group_stage_id')
            ->where($where)
            ->field($fields)
            ->order('ao.create_at desc')
            ->page($page, $size)
            ->select();
        return Functions::result(
            [
                'total' => 0,
                'list'  => $list,
            ]
        );
    }

    /**
     * @purpose 获取退款订单,包括全部退款和部分退款
     * @param int $page
     * @param int $size
     * @param int $uid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private static function refundOrderItems (int $page, int $size, int $uid): array
    {
        $model   = new ActivityOrder();
        $fields  = [
            'ao.id',
            'ao.activity_id',
            'ao.order_no',
            'ao.status',
            'a.title',
            'a.descriptions',
            'a.thumb',
            'a.light_spot',
            'FROM_UNIXTIME(ao.pay_time, "%Y-%m-%d %H:%i") as sign_up_date',
            'FROM_UNIXTIME(ao.create_at, "%Y-%m-%d %H:%i") as create_date',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 1)' => 'adult_num',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id` and `travel_type` = 2)' => 'kid_num',
            '(select count(1) as `total` from `activity_order_attach` where (`status` = 3 or `status` = 4 or `status` = 5) and `order_id`=`ao`.`id`)' => 'refund_num',
            '(select count(1) as `total` from `activity_order_attach` where `order_id`=`ao`.`id` and `travel_type` = 2)' => 'total_num',
            '(select sum(price) as `total_price` from `activity_order_attach` where (`status` = 99 or `status` = 1) and `order_id`=`ao`.`id`)'               => 'total_price',
        ];
        $subWhere   = [];
        $subWhere[] = ['ao.uid', '=', $uid];
        $subWhere[] = ['ao.status', 'in', [3,5,99]];
        $subQuery = $model
            ->alias('ao')
            ->leftJoin('activity_group_stage ags', 'ags.id=ao.group_stage_id')
            ->join(\app\model\Activity::M()->table.' a', 'a.id=ao.activity_id')
            ->where($subWhere)
            ->field($fields)
            ->order('ao.create_at desc')
            ->buildSql();
        $list = Db::table($subQuery. 's')
            ->where('refund_num', '>', 0)
            ->page($page, $size)
            ->select();
        foreach ($list as &$item) {
            $item['activity'] = [];
            $item['activity']['id'] = $item['activity_id'];

            $item['activity']['title'] = $item['title'];
            $item['activity']['descriptions'] = $item['descriptions'];
            $item['activity']['thumb'] = $item['thumb'];
            $item['activity']['light_spot'] = json_decode($item['light_spot'], true);
            unset($item['title']);
            unset($item['descriptions']);
            unset($item['thumb']);
            unset($item['light_spot']);
        }
        return Functions::result(
            [
                'total' => 0,
                'list'  => $list,
            ]
        );
    }
    /**
     * @purpose 获取最新出行订单
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function newOrderItems(int $uid): array
    {
        $model  = new ActivityOrder();
        $fields = 'ao.id,ao.order_no,ao.create_at,ao.activity_id,
        FROM_UNIXTIME(ags.start_time, "%Y.%m.%d") as start_date,ags.adult_price,
        FROM_UNIXTIME(ags.end_time, "%Y.%m.%d") as end_date,
        FROM_UNIXTIME(ao.pay_time, "%Y-%m-%d %H:%i") as sign_up_date,
        ags.kid_price,(FLOOR(((ags.end_time - ags.start_time) / 86400)) + 1) as days';
        $result = $model
            ->with(['attach' => function ($query) {
                $query
                    ->where(['status' => 99])
                    ->field('id as order_attach_id,order_id,group_stage_id,collection_point_id,fullname,
                travel_type,travel_sex,travel_mobile,travel_certificate_type,travel_certificate_number,price,status');
            }, 'activity'    => function ($query) {
                $query
                    ->alias('a')
                    ->leftJoin('club c', 'c.uid=a.club_id')
                    ->field('a.id,a.title,a.thumb,a.light_spot,a.club_id,c.name as club_name');
            }])
            ->alias('ao')
            ->leftJoin('activity_group_stage ags', 'ags.id=ao.group_stage_id')
//            ->leftJoin('club c', 'c.uid=a.club_id')
            ->where([
                ['ao.status', '=', 99],
                ['ao.uid', '=', $uid],
                ['ags.start_time', '>', time()],
            ])
            ->field($fields)
            ->select();
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $result
        );
    }

    /**
     * @purpose 获取不同状态订单数量
     * @return array
     */
    public static function total(int $uid): array
    {
        $model = new ActivityOrder();
        // 获取待付款订单数量
        // 获取报名成功订单数量
        $total            = [];
        $total['wait']    = $model
            ->alias('ao')
            ->join('activity_group_stage ags', 'ags.id=ao.group_stage_id')
            ->where([
                ['ao.status', '=', 1],
                ['ao.uid', '=', $uid],
                ['ags.start_time', '>', time()],
            ])
            ->count(1);
        $total['success'] = $model
            ->alias('ao')
            ->join('activity_group_stage ags', 'ags.id=ao.group_stage_id')
            ->where([
                ['ags.start_time', '>', time()],
                ['ao.status', '=', 99],
                ['ao.uid', '=', $uid],
            ])
            ->count(1);
        return Functions::result(
            STATUS_CODE::SUCCESS,
            $total
        );
    }

    /**
     * @purpose 创建订单
     * @param array $data = [
     *      'group_stage_id' => '',
     *      'travel' => [
     *          [
     *              'fullname' => '',
     *              'id' => '',
     *              'collection_point_id' => ''
     *          ]
     *      ]
     * ]
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public static function create(array $data, int $uid): array
    {
        if (!isset($data['group_stage_id']) || empty($data['group_stage_id'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '参数错误!'
            );
        }
        if (!isset($data['travel']) || empty($data['travel'])) {
            return Functions::result(
                STATUS_CODE::PARAMETER_ERROR,
                '请至少添加一个出行人...!'
            );
        }
        $travel_ids           = [];
        $collection_point_ids = [];
        foreach ($data['travel'] as $travel) {
            if (!isset($travel['collection_point_id'])) {
                return Functions::result(
                    STATUS_CODE::PARAMETER_ERROR,
                    '出行人 [' . $travel['fullname'] . '] 缺少集合点'
                );
            }
            array_push($travel_ids, $travel['id']);
            array_push($collection_point_ids, $travel['collection_point_id']);
        }
//        $collection_point = new ActivityCollectionPoint();
        $travel       = new \app\model\TravelPeople();
        $group_stage  = new ActivityGroupStage();
        $order        = new ActivityOrder();
        $order_attach = new ActivityOrderAttach();
        $order->startTrans();
        // 获取团期价格
        $group_stage_info = $group_stage
            ->where('id', $data['group_stage_id'])
            ->field('id,start_time,end_time,activity_id,apply_end_date,adult_price,kid_price,insurance_price,insurance_price')
            ->find();
        if (!$group_stage_info) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '团期不存在...!'
            );
        }
        if ($group_stage_info->apply_end_date < time()) {
            return Functions::result(
                STATUS_CODE::FAIL,
                '该团期已过最后报名时间..!'
            );
        }
        $price_info = [
            1 => $group_stage_info->adult_price,
            2 => $group_stage_info->kid_price,
        ];
        // 构建订单主表数据
        $order_data                   = [];
        $order_data['uid']            = $uid;
        $order_data['activity_id']    = $group_stage_info->activity_id;
        $order_data['group_stage_id'] = $data['group_stage_id'];
        $order_data['order_no']       = date('YmdHis', time()) . Functions::random('8');
        RE_ORDER_NO:
        $order_data['order_no'] = date('YmdHis', time()) . Functions::random('8');
        if ($order->where('order_no', $order_data['order_no'])->count(1) > 0) {
            goto RE_ORDER_NO;
        }
        // 创建订单数据
        $order_result = $order
            ->isUpdate(false)
            ->data($order_data)
            ->save();
        $order_id     = $order->id;
        // 获取出行人信息
        $travel_list  = $travel
            ->where('id', 'in', $travel_ids)
            ->field('id,name,sex,type,mobile,certificate_type,certificate_number')
            ->select()
            ->toArray();
        $_travel_list = [];
        foreach ($travel_list as $val) {
            $_travel_list[$val['id']] = $val;
        }
        // 构建订单附表数据
        $order_attach_data = [];
        foreach ($data['travel'] as $travel) {
            array_push($order_attach_data, [
                'order_id'                  => $order_id,
                'group_stage_id'            => $data['group_stage_id'],
                'activity_id'               => $group_stage_info->activity_id,
                'collection_point_id'       => $travel['collection_point_id'],
                'fullname'                  => $_travel_list[$travel['id']]['name'],
                'travel_type'               => $_travel_list[$travel['id']]['type'],
                'travel_sex'                => $_travel_list[$travel['id']]['sex'],
                'travel_mobile'             => $_travel_list[$travel['id']]['mobile'],
                'travel_certificate_type'   => $_travel_list[$travel['id']]['certificate_type'],
                'travel_certificate_number' => $_travel_list[$travel['id']]['certificate_number'],
                'price'                     => $price_info[$_travel_list[$travel['id']]['type']],
            ]);
        }
        $order_attach_result = $order_attach
            ->isUpdate(false)
            ->saveAll($order_attach_data);
        if ($order_result && $order_attach_result->count() === count($order_attach_data)) {
            $order->commit();
            return Functions::result(
                [
                    'order_no' => $order->order_no,
                ]
            );
        }
        $order->rollback();
        return Functions::result(
            STATUS_CODE::CREATE_FAIL,
            '订单创建失败...!'
        );
    }
}